package net.lab0.kanjidic;

import java.io.File;
import java.io.IOException;

import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.ParsingException;
import nu.xom.ValidityException;

public class Main
{
    public static void main(String[] args)
    throws ValidityException, ParsingException, IOException
    {
        Builder builder = new Builder();
        Document doc = builder.build(new File("./rc/kanjidicClean.xml"));
        
        
        
        Element root = doc.getRootElement();
        Elements characters = root.getChildElements("character");
        int displayedChar = 0;
        for (int i = 0; i < characters.size(); ++i)
        {
            Element character = characters.get(i);
            Element literal = character.getFirstChildElement("literal");
            Element misc = character.getFirstChildElement("misc");
            Element grade = misc.getFirstChildElement("grade");
            Element readMean = character.getFirstChildElement("reading_meaning");
            if (readMean == null)
            {
                continue;
            }
            Element rmGroup = readMean.getFirstChildElement("rmgroup");
            Elements readings = rmGroup.getChildElements("reading");
            
            String on = "";
            String kun = "";
            for (int j = 0; j < readings.size(); ++j)
            {
                Element reading = readings.get(j);
                String rType = reading.getAttribute("r_type").getValue();
                
                if ("ja_on".equals(rType))
                {
                    on += " " + reading.getValue();
                }
                if ("ja_kun".equals(rType))
                {
                    kun += " " + reading.getValue();
                }
            }
            
            Elements meanings = rmGroup.getChildElements("meaning");
            String frTranslations = "";
            String enTranslations = "";
            for (int j = 0; j < meanings.size(); ++j)
            {
                Element meaning = meanings.get(j);
                Attribute mLang = meaning.getAttribute("m_lang");
                
                if (mLang == null)
                {
                    enTranslations += " " + meaning.getValue();
                }
                else if ("fr".equals(mLang.getValue()))
                {
                    frTranslations += " " + meaning.getValue();
                }
            }
            if (frTranslations.equals(""))
            {
                if (enTranslations == "")
                {
                    continue;
                }
                else
                {
                    frTranslations = enTranslations;
                }
            }
            
            String kanji = literal.getValue().substring(0, 1);
            String level = "";
            if (grade != null)
            {
                level = grade.getValue();
            }
            
            System.out.println(kanji + " level" + level + " on=" + on + " kun=" + kun + " fr=" + frTranslations);
            displayedChar++;
        }
        
        System.out.println("end " + displayedChar);
    }
}
